﻿
using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Car
{
    public  class LineAppService : ApplicationService, ILineAppService
    {
        private readonly IRepository<Line, int> _OwnerRepository;
        private readonly LogisticsDbContext db;
        private readonly ILineRepository _IFieIdRepository;
        public LineAppService(IRepository<Line, int> OwnerRepository, ILineRepository IOwnerRepository, LogisticsDbContext dbc)
        {

            _OwnerRepository = OwnerRepository;
            _IFieIdRepository = IOwnerRepository;
            db = dbc;
        }
        /// <summary>
        /// 货主添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public async Task<LineDto> Create(LineDto m)
        {
            var Owner = new Line
            {
               DesId=m.DesId,
                Name=m.Name,
                Oid=m.Oid,
                Ldetail=m.Ldetail,
                Did=m.Did,
                Ldestination=m.Ldestination,
                Uid=m.Uid,
                Lshippername=m.Lshippername,
                Lownerphone=m.Lownerphone,
                Lunit=m.Lunit,
                Lremark=m.Lremark,
                Ldate=m.Ldate
            };

            await _IFieIdRepository.Create(Owner);
            return ObjectMapper.Map<Line, LineDto>(Owner);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task DeleteAsync(int id)
        {
            await _IFieIdRepository.Delete(id);
        }

        public async Task<LineDto> GetCarryById(int Id)
        {
            var student = await _IFieIdRepository.GetCarryById(Id);
            return ObjectMapper.Map<Line, LineDto>(student);
        }

        public async Task<PagedResultDto<LineDto>> GetListAsync(GetLineDto dto)
        {
            var vehiclelist = await _OwnerRepository.GetPagedListAsync(
               dto.SkipCount,
               dto.MaxResultCount,
               dto.Sorting);
            var totalCount = await AsyncExecuter.CountAsync(
               _OwnerRepository.WhereIf(
                   !dto.Filter.IsNullOrWhiteSpace(),
                   m => m.Name.Contains(dto.Filter)
               )
           );
            return new PagedResultDto<LineDto>(
             totalCount,
             ObjectMapper.Map<List<Line>, List<LineDto>>(vehiclelist));
        }

       

        public async Task<int> Update(LineDto ca)
        {
            var carr = await _OwnerRepository.GetAsync(ca.Id);
            carr.DesId = ca.DesId;
            carr.Name = ca.Name;
            carr.Oid = ca.Oid;
            carr.Ldetail = ca.Ldetail;
            carr.Did = ca.Did;
            carr.Ldestination = ca.Ldestination;
            carr.Uid = ca.Uid;
            carr.Lshippername = ca.Lshippername;
            carr.Lownerphone = ca.Lownerphone;
            carr.Lunit = ca.Lunit;
            carr.Lremark = ca.Lremark;
            carr.Ldate = ca.Ldate;

            await _OwnerRepository.UpdateAsync(carr);
            return 1;
        }
        public async Task<List<LongitudeandlatitudeDto>> GetLong(int Id)
        {
            var list = db.Longitudeandlatitude.Where(m=>m.Lid==Id).ToList();
            return ObjectMapper.Map<List<Longitudeandlatitude>, List<LongitudeandlatitudeDto>>(list);
        }

    }
}
